import Vue from 'vue'
import VueRouter from 'vue-router'
import { getLocalStorage } from '@/utils/storage'
// import { adminMessage } from '@/api/user'
import { Notification } from 'element-ui'
Vue.use(VueRouter)
const Home = () => import('@/views/home-index.vue')
const Layout = () => import('@/views/Layout/index.vue')
const Login = () => import('@/views/Login/index.vue')
const Dashboard = () => import('@/views/Dashboard/index.vue')
const Article = () => import('@/views/Article/index.vue')
const routes = [
  {
    path: '/',
    redirect: 'Layout'
  },
  {
    path: '/home',
    name: 'Home',
    component: Home
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/layout',
    name: 'Layout',
    component: Layout,
    children: [
      {
        path: 'dashboard',
        component: Dashboard,
        name: 'Dashboard'
      },
      {
        path: 'article',
        component: Article,
        name: 'Article'
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

const whileList = ['/login']
router.beforeEach((to, from, next) => {
  if (getLocalStorage('token')) {
    return next()
  }
  if (whileList.indexOf(to.path) !== -1) {
    return next()
  }

  next('/login')
  Notification({
    title: '警告',
    message: '请先登录',
    type: 'warning',
    duration: 1000
  })
})
export default router
